﻿//设置cookie
function setCookie(c_name,value,expiredays)
{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

//取回cookie
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}

$(document).ready(function(){

    var _coursewareId = $("#courseware-id").html();
    var _userId;
    var _video = document.getElementsByTagName("video")[0];
    //var _video = document.getElementById("videoplayer");
    var _course;
    var _breakpoint;
    var _currentTime;
    var _cookieName = "xwsqxy_courseware_" + _coursewareId;
    var _isEnrolled = false;
    var _isLogged = false;

    //检查用户cookie是否存在
    if(!$.cookie('xwsqxy_account')){

        //若不存在则显示登录
        $("#btn-login").show();
        $("#btn-wantcourse").hide();
        
        console.log("user status : logout.");

    }else{
        //若存在则显示用户名和退出
        var _account = JSON.parse($.cookie('xwsqxy_account'));
        _userId = _account.id;

        $("#btn-login").hide();
        $("#courseware_cover").hide();
        $("#btn-wantcourse").show();
        $(".vjs-default-skin").show();

        axios.get('/eduonline/api/coursewares/enroll?userId=' + _userId + '&coursewareId=' + _coursewareId)
                .then(function(response){
                    if(response.status == '200'){
                        _isEnrolled = true;
                    }
                }).catch(function (error){
                    if(error.response.status == '404'){
                        _isEnrolled = false;
                    }

                    console.log(error.response.data.message);
                });
    }

    //初始化课程cookie
    function initCourse(){

    	//检查课程cookie是否存在
    	if(getCookie(_cookieName) === ""){

    		//若不存在则创建课程cookie
    		setCookie(_cookieName, "0", 30);
    		
    	}else{
    		//_video.play();
    		//_video.pause();
    		//_video.currentTime = 30;
    		//_video.currentTime = parseInt(getCookie(_cookieName));
    		//console.log(getCookie(_cookieName));
    	}
    	console.log("current breakpoint : " + getCookie(_cookieName));

    }

    initCourse();

    

    //视频播放器播放位置更改事件处理
    //以30秒为周期更新课程cookie
    _video.addEventListener("timeupdate", function(){
        
    	if(parseInt(_video.currentTime) > 0){
            _currentTime = parseInt(_video.currentTime);//获取当前播放时间
            if(_currentTime%30 === 0){
                setCookie(_cookieName, _video.currentTime, 30);

                console.log("video status : timeupdate");
        		console.log("current breakpoint : " + getCookie(_cookieName));
            }
        }

        if(parseInt(getCookie(_cookieName)) > _video.currentTime + 10){
        	_video.currentTime = getCookie(_cookieName);
        }
    });

    //视频播放器结束事件处理
    _video.addEventListener("ended", function(){
        
        setCookie(_cookieName, 0, 30);
        $(this).attr("disabled",false);
	    $(this).css("pointer-events","point");

        console.log("video status : ended");
        console.log("current breakpoint : " + getCookie(_cookieName));

    });

    //视频播放器快进事件处理
    _video.addEventListener("seeked", function(){
        
        setCookie(_cookieName, _video.currentTime, 30);

        console.log("video status : seeked");
        console.log("current breakpoint : " + getCookie(_cookieName));

    });

    //视频播放器暂停事件处理
    _video.addEventListener("pause", function(){
        
        _currentTime = parseInt(_video.currentTime);
        setCookie(_cookieName, _video.currentTime, 30);

        console.log("video status : pause");
        console.log("current breakpoint : " + getCookie(_cookieName));

    });

	//视频播放器播放事件处理
    _video.addEventListener("play", function(){
    	if(!_isEnrolled){
            
            axios.post('/eduonline/api/coursewares/enroll', {userId: _userId, coursewareId: _coursewareId})
                .then(function(response){
                    _isEnrolled = true;
                    console.log(response.data);
                })
                .catch(function(error){
                    _isEnrolled = false;
                    console.log(error.response.data.message);
                });
        }
        if(!_isLogged){
    		axios.post('/eduonline/api/coursewares/log', {userId: _userId, coursewareId: _coursewareId})
                .then(function(response){
                    _isRecorded = true;
                    console.log(response.data);
                })
                .catch(function(error){
                    _isLogged = false;
                    console.log(error.response.data.message);
                });
    	}
    });


    $("#btn-wantcourse").click(function(){
        _video.play();
        $(this).attr("disabled",true);
	    $(this).css("pointer-events","none");

        console.log("video status : play");
    	console.log("current breakpoint : " + getCookie(_cookieName));
    });
});